package com.zelin.web.exception;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ExceptionHandler;

public class GlobalExceptionHandler {

    //处理MyException异常
    @ExceptionHandler(MyException.class)
    public String error(MyException ex, Model model){
        //定义异常信息
        String msg = "自定义异常！";
        //判断ex是否有值
        if(ex != null){
            msg = ex.getMessage();
        }
        //将错误信息放到model中
        model.addAttribute("msg",msg);
        //返回
        return "error";
    }

    //处理其它异常
    @ExceptionHandler(Exception.class)
    public String exceptionError(Exception ex, Model model){
        //定义异常信息
        String msg = "未知异常！";
        //判断ex是否有值
        if(ex != null){
            msg = ex.getMessage();
        }
        //将错误信息放到model中
        model.addAttribute("msg",msg);
        //返回
        return "error";
    }
}
